﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;

namespace VisualMove.Data
{
    public class MoveList
    {
        static MoveList()
        {
            MoveListCollection = new Collection<Move>();
        }

        public static Collection<Move> MoveListCollection
        {
            get;
            set;
        }

        public static Move CurrentMove
        {
            get;
            set;
        }

        public static async Task<Move> FindMove(string sName)
        {
            CurrentMove = MoveListCollection.FirstOrDefault(oMove => oMove.Name == sName);
            if (CurrentMove == null)
            {
                StorageFolder oMoveFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(sName);
                CurrentMove = new Move(oMoveFolder);
                MoveListCollection.Add(CurrentMove);
            }

            return CurrentMove;
        }

        public static async Task LoadFolders()
        {
            CurrentMove = null;
            MoveListCollection.Clear();
            IReadOnlyList<StorageFolder> oFolders = await ApplicationData.Current.LocalFolder.GetFoldersAsync();

            foreach (StorageFolder oFolder in oFolders)
            {
                Move oMove = new Move(oFolder);
                MoveListCollection.Add(oMove);
                oMove.LoadFolders();
            }
        }

        public static async Task DeleteCurrentMove()
        {
            if (CurrentMove != null)
            {
                await CurrentMove.MoveFolder.DeleteAsync();
                MoveListCollection.Remove(CurrentMove);
            }
            CurrentMove = null;
        }

        public static async Task Clear()
        {
            int iMoves = MoveListCollection.Count();
            for (int i = 0; i < iMoves; i++)
            {
                await MoveListCollection[i].MoveFolder.DeleteAsync();
            }
            CurrentMove = null;
        }
    }   
}
